package com.hongxin.nw.netty.model.packing;

import com.hongxin.nw.netty.utils.BinaryTools;
import com.hongxin.nw.netty.utils.FieldPack;
import com.hongxin.nw.netty.values.StaticValue;
import com.hongxin.nw.netty.values.TypeIdentif;
import lombok.extern.slf4j.Slf4j;

/**
 * @remark
 * @author: GuoXiaolin
 * @date 2019/5/17 15:48
 * @Version 1.0
 * @Descibe
 */
@Slf4j
public class ASDU {

    private String type;//在7.2.1中定义	类型标识（TYPE）	数据单元 标识符
    private String vsq;//在7.2.2中定义	可变结构限定词（VSQ）
    private String cot;//在7.2.3中定义	传送原因（COT）
    private String staadr;//在7.2.4中定义	应用服务数据单元公共地址
    private String infoAdr;//在7.2.5中定义	信息对象地址	信息对象

    public String getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = Integer.toHexString(type);
    }

    public String getVsq() {
        return vsq;
    }

    public void setVsq(Integer num,Integer sq) {
        String svsq = sq + BinaryTools.addZero(Integer.toBinaryString(num),7,StaticValue.DIRECTION_LEFT);
        this.vsq =  BinaryTools.addZero(Integer.toHexString(Integer.parseInt(svsq,2)), TypeIdentif.BYTE_VSQ.getByteLength(),StaticValue.DIRECTION_LEFT);
    }

    public String getCot() {
        return cot;
    }

    public void setCot(Integer cot) {
        this.cot = FieldPack.caseToBin(String.valueOf(cot), TypeIdentif.BYTE_COT.getByteLength());
    }

    public String getStaadr() {
        return staadr;
    }

    public void setStaadr(String staadr) {
        this.staadr = BinaryTools.converter(BinaryTools.addZero(Integer.toHexString(Integer.parseInt(staadr)), TypeIdentif.BYTE_STAT_ADDRESS.getByteLength(),StaticValue.DIRECTION_LEFT));
    }

    public String getInfoadr() {
        return infoAdr;
    }

    public void setInfoadr(Integer infoAdr) {
        //信息对象地址待处理,,暂时使用000000测试
//        this.infoAdr = BinaryTools.addZero(String.valueOf(infoAdr),TypeIdentif.BYTE_INFO_ADDRESS.getByteLength(),StaticValue.DIRECTION_LEFT);
        this.infoAdr = Integer.toHexString(infoAdr);
        this.infoAdr = BinaryTools.addZero(this.infoAdr, TypeIdentif.BYTE_INFO_ADDRESS.getByteLength(), StaticValue.DIRECTION_RIGHT);
        this.infoAdr = BinaryTools.converter(this.infoAdr);
    }

    public String getHexData(){
        return type + vsq + cot + staadr + infoAdr;
    }

    @Override
    public String toString() {
        return "ASDU{" +
                "type='" + type + '\'' +
                ", vsq='" + vsq + '\'' +
                ", cot='" + cot + '\'' +
                ", staadr='" + staadr + '\'' +
                ", infoAdr='" + infoAdr + '\'' +
                '}';
    }
}
